package crm.control;

import java.sql.ResultSet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;

import crm.dbcoon.DBConnect;
import crm.model.Recepit_description;

public class RecepitDescriptionControl {
	//判断跟进时间
	public static long dateLong(String date,java.util.Date d2) {
		//String date = RecepitDescriptionControl.getDescriptionById(id).getDate();
		SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd");
		//java.util.Date d2 = new java.util.Date();
		long nd = 1000*24*60*60;//一天的毫秒数
		try {
			long d = (d2.getTime()-dt.parse(date).getTime())/nd;
			System.out.println(d);
			return d;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}
	}
	/**
	 * 
	 * @param rDescription
	 * @return
	 */
	public static Boolean addRecepitDescription(Recepit_description rDescription) {
		DBConnect dbc=new DBConnect();
		String sql="insert into recepit_description(recepit_id,follow_person_id,date,content)" + 
		"values(?,?,?,?)";
		dbc.prepareStatement(sql);
		int index=1;
		try {
			dbc.setInt(index++, rDescription.getRecepit_id());
			dbc.setInt(index++, rDescription.getFollow_person_id());
			dbc.setString(index++, rDescription.getDate());
			dbc.setString(index++, rDescription.getContent());
			dbc.executeUpdate();
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
	}
	/**
	 * 
	 * @param recepitDescription
	 * @return
	 */
	public static Boolean updateRecepitDescription(Recepit_description recepitDescription) {
		DBConnect dbc = new DBConnect();
		String sql = "update recepit_description set recepit_id=?,follow_person_id=?,date=?,content=? where id='"+recepitDescription.getId()+"'";
		dbc.prepareStatement(sql);
		try {
			setRs(dbc, recepitDescription);
			dbc.executeUpdate();
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
	}
	/**
	 * 
	 * @param dbc
	 * @param recepitDescription
	 */
	public static void setRs(DBConnect dbc,Recepit_description recepitDescription) {
		int index=1;
		try {
			dbc.setInt(index++, recepitDescription.getRecepit_id());
			dbc.setInt(index++, recepitDescription.getFollow_person_id());
			dbc.setString(index++, recepitDescription.getDate());
			dbc.setString(index++, recepitDescription.getContent());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("setRs出错");

		}finally{
			dbc.close();
		}
	}
	/**
	 * 
	 * @param rs
	 * @param recepitDescription
	 */
	public static void getRs(ResultSet rs,Recepit_description recepitDescription) {
		try {
			recepitDescription.setRecepit_id(rs.getInt("recepit_id"));
			recepitDescription.setFollow_person_id(rs.getInt("follow_person_id"));
			recepitDescription.setDate(rs.getString("date"));
			recepitDescription.setContent(rs.getString("content"));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("getRs方法出错");
		}
		
	}
	/**
	 * 
	 * @param recepitId
	 * @return
	 */
	public static Collection<Recepit_description> getAllRecepitDescriptions(int recepitId) {
		Collection<Recepit_description> rd = new ArrayList<Recepit_description>();
		DBConnect dbc = new DBConnect();
		String sql = "select * from recepit_description where recepit_id='"+recepitId+"'";
		dbc.prepareStatement(sql);
		ResultSet rs = null;
		try {
			rs = dbc.executeQuery();
			while (rs.next()) {
				Recepit_description  recepitDescription= new Recepit_description();
				getRs(rs, recepitDescription);
				rd.add(recepitDescription);	
			}
			return rd;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}finally{
			dbc.close();
		}
	}
	/**
	 * 
	 * @param id
	 * @return
	 */
	public static Recepit_description getDescriptionById(int id) {
		DBConnect dbc = new DBConnect();
		Recepit_description rd = new Recepit_description();
		String sql = "select * from recepit_description where id = '"+id+"'";
		dbc.prepareStatement(sql);
		ResultSet rs = null;
		try {
			rs=dbc.executeQuery();
			while (rs.next()) {
				rd.setDate(rs.getString("date"));
			}
			return rd;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}finally{
			dbc.close();
		}
	}
	public static String getMaxDescriptionTime(int recepit_id) {
		DBConnect dbc = new DBConnect();
		//System.out.println(recepit_id+"jjj");
		String date = "0000-00-00";
		String sql = "select max(date) from recepit_description,control_recepit where control_recepit.id = recepit_description.recepit_id and recepit_id = '"+recepit_id+"'";
		dbc.prepareStatement(sql);
		ResultSet rs = null;
		try {
			rs = dbc.executeQuery();
			while (rs.next()) {
				date=rs.getString(1);
			}
			//System.out.println(date);
			return date;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return date;
		}finally{
			dbc.close();
		}
	}

}
